package common.dao.couchdb;

import com.fasterxml.jackson.databind.node.ObjectNode;
import common.config.DbTableNames;
import common.helper.toolkit.HttpToolkit;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.net.MalformedURLException;


/**
 * Created by Common on 7/24/16.
 */
/*
https://github.com/helun/Ektorp
 */
@Component
public class CouchDBDOCFactory {

    final Logger logger = LoggerFactory.getLogger(CouchDBDOCFactory.class);

    private final String config_path = "/couchdb_admin/doc_http_servers.json";


    private CouchDbInstance couchdb;
    private CouchDbConnector video_con;
    private CouchDbConnector program_con;
    private CouchDbConnector sequence_con;
    private CouchDbConnector scene_con;
    private CouchDbConnector shot_con;

    //@Inject
    public CouchDBDOCFactory() {

        String doc_url = HttpToolkit.getHostPort(this.config_path);
        logger.info("[CouchDBKGFactory] url = "  + doc_url);

        HttpClient httpClient = null;
        try {
            httpClient = new StdHttpClient.Builder()
                    .url( doc_url )
                    .build();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        this.couchdb = new StdCouchDbInstance(httpClient);
        //video
        this.video_con = new StdCouchDbConnector(DbTableNames.DOCCouchDB.VIDEO_TB, this.couchdb);
        this.video_con.createDatabaseIfNotExists();
        //program
        this.program_con = new StdCouchDbConnector(DbTableNames.DOCCouchDB.PROGRAM_TB , this.couchdb);
        this.program_con.createDatabaseIfNotExists();
        //sequence
        this.sequence_con = new StdCouchDbConnector(DbTableNames.DOCCouchDB.SEQUENCE_TB , this.couchdb);
        this.sequence_con.createDatabaseIfNotExists();
        //scene
        this.scene_con = new StdCouchDbConnector(DbTableNames.DOCCouchDB.SCENE_TB , this.couchdb);
        this.scene_con.createDatabaseIfNotExists();
        //shot
        this.shot_con = new StdCouchDbConnector(DbTableNames.DOCCouchDB.SHOT_TB , this.couchdb);
        this.shot_con.createDatabaseIfNotExists();

    }

    @Override
    public void finalize(){
        //
    }


    public CouchDbInstance getCouchDB() {
        return this.couchdb;
    }

    public CouchDbConnector getVideoCon(){
        return this.video_con;
    }

    public CouchDbConnector getProgramCon(){
        return this.program_con;
    }

    public CouchDbConnector getSequenceCon(){
        return this.sequence_con;
    }

    public CouchDbConnector getSceneCon(){
        return this.scene_con;
    }

    public CouchDbConnector getShotCon(){
        return this.shot_con;
    }





    /*************** backup *************************/
    public static void main(String[] args){
        CouchDbConnector concept_con = new CouchDBDOCFactory().getVideoCon();
        /*
        Sofa sofa = db.get(Sofa.class, "ektorp");
        sofa.setColor("blue");
        db.update(sofa);
        */
        /*
        ViewQuery query = new ViewQuery()
                .designDocId("_design/somedoc")
                .viewName("view_with_huge_result");

        InputStream data = couchdb.queryForStream(query);
        String ret = "";
        try {
            ret = FileToolkit.readStringFromStream(data);
            data.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(ret);
        */

        /*
        List<String> ids = couchdb.getAllDocIds();
        for(String id : ids){
            System.out.println(id);
        }
        */
        ObjectNode obj = concept_con.get(ObjectNode.class , "concept-19885");
        System.out.println(obj.toString());
        System.out.println("_id = " + obj.get("_id"));


    }





}
